package java8.stream;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * @Description：
 * @Author：LiDong
 * @Create：2021/2/27
 * @Version：1.0.0
 */
public class StreamTestOne {

    private static final Logger logger = LoggerFactory.getLogger(StreamTestOne.class);

    @Test
    public void test1() {
        List<String> list = Arrays.asList("zhangsan", "", "lisi", "wangwu");
        list.forEach(System.out::println);

        List<String> collect = list.stream().filter("lisi"::equals).map(String::toUpperCase).collect(Collectors.toList());
        collect.forEach(System.out::println);

    }

    @Test
    public void test2() {
        Random random = new Random();
        random.ints().limit(20).sorted().forEach(System.out::println);

        List<String> strings = Arrays.asList("abc", "", "bc", "efg", "abcd", "", "jkl");
        // 获取空字符串的数量
        long count = strings.parallelStream().filter(String::isEmpty).count();
    }

    @Test
    public void test3() {
        List<Integer> list = Arrays.asList(1, 3, 4, 5, 6);
        long count = list.size();
        int sum = list.stream().mapToInt(x -> x).sum();
        logger.info(String.valueOf(sum));
        logger.info(String.valueOf(count));
    }

    /**
     * Stream 流快速求和
     */
    @Test
    public void test4() {
        List<Double> doubleList = Arrays.asList(12.2, 13.45, 12.4);
        double sumOne = doubleList.stream().mapToDouble(x -> x).sum();
        logger.info("sumOne:{}", sumOne);

        List<BigDecimal> decimalList = new ArrayList<>();
        decimalList.add(new BigDecimal("12.3"));
        decimalList.add(new BigDecimal("12.3"));
        decimalList.add(new BigDecimal("12.3"));
        BigDecimal bigDecimal = decimalList.stream().reduce(BigDecimal.ZERO, BigDecimal::add);
        logger.info("bigDecimal:{}", bigDecimal.floatValue());
        logger.info("bigDecimal:{}", bigDecimal.doubleValue());

        List<Object> objectList = Arrays.asList(12, 13, 1);
        int sum = objectList.stream().mapToInt(x -> (int) x).sum();
        logger.info("sum:{}", sum);
    }

    /**
     * stream 中字段 匹配
     */
    @Test
    public void test5() {
        List<Integer> list = Arrays.asList(1, 32, 43, 5, 6);
        boolean flag = list.stream().anyMatch(x -> x.equals(3));
        logger.info(String.valueOf(flag));
        System.out.println("test");
    }


}
